/***
 * express 日志中间件
 * @type {*|createApplication}
 */
const express = require('express'), app = express();
const winston = require('winston'),
    expressWinston = require('express-winston');
require('winston-daily-rotate-file');

const logFileTransport = new (winston.transports.DailyRotateFile)({
    filename: './log/application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
});

app.use(expressWinston.logger({
    transports: [
        new winston.transports.Console({
            colorize: true
        }),
        logFileTransport
    ],
    meta: false, // optional: control whether you want to log the meta data about the request (default to true)
    msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
    expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
    colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
    ignoreRoute: function (req, res) {
        return false;
    } // optional: allows to skip some log messages based on request and/or response
}));

app.use(expressWinston.errorLogger({
    level:"error",
    transports: [
        new winston.transports.Console({
            colorize: true
        }),
        logFileTransport
    ]
}));

export default app;